<?php
/**
 * Api基类
 */

namespace App;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\WebSocketClient\ClientFactory;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerInterface;

abstract class ApiBaseController
{
    protected $_client = null;

    /**
     * @Inject
     * @var ClientFactory
     */
    protected $clientFactory;

    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject
     * @var ResponseInterface
     */
    protected $response;

    /**
     * appid
     * @var int
     */
    protected $_appid = 0;

    public function __construct(){
        $this->_appid = (int)$this->request->input('appid');

        // 对端服务的地址，如没有提供 ws:// 或 wss:// 前缀，则默认补充 ws://
        $host = '127.0.0.1:9502' . '?from=server&appid=';
        // 通过 ClientFactory 创建 Client 对象，创建出来的对象为短生命周期对象
        if ($this->_client === null){
            $this->_client = $this->clientFactory->create($host,false);
        }

    }

    /**
     * 统一返回
     * @param int $code
     * @param array $data
     * @param string $msg
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function res_json($code=0,$data=[],$msg='')
    {
        return $this->response->json(
            [
                'code' => $code,
                'msg'  => $msg,
                'data' => $data,
                'time' => time()
            ]);
    }

    /**
     * 统一返回 错误
     * @param int $code
     * @param array $data
     * @param string $msg
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function res_success($data=[], $code=0, $msg='success')
    {
        return $this->response->json(
            [
                'code' => $code,
                'msg'  => $msg,
                'data' => $data,
                'time' => time()
            ]);
    }

    /**
     * 统一返回 错误
     * @param int $code
     * @param array $data
     * @param string $msg
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function res_error($msg='error',$code=-1,$data=[])
    {
        return $this->response->json(
            [
                'code' => $code,
                'msg'  => $msg,
                'data' => $data,
                'time' => time()
            ]);
    }

    /**
     * @param $info
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function res_response($info)
    {
        $info['appid'] = $this->_appid;

        $this->_client->push(json_encode($info));

        // 获取服务端响应的消息，服务端需要通过 push 向本客户端的 fd 投递消息，才能获取；
        // 以下设置超时时间 2s，接收到的数据类型为 Frame 对象。
        $msg = $this->_client->recv(2);
        $res_arr = json_decode($msg->data,true);

        if (is_array($res_arr))
        {
            return $this->response->json($res_arr);
        }else {
            return $this->res_json(-500,[],'服务端异常');
        }
    }
}